package com.veer.shop.service.impl.admin;

import java.util.ArrayList;
import java.util.List;

import com.veer.shop.result.ListDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.veer.shop.config.ResponseCodeEnum;
import com.veer.shop.mapper.AdminMenuMapper;
import com.veer.shop.param.AdminMenuParam;
import com.veer.shop.pojo.AdminMenu;
import com.veer.shop.pojo.ResponseData;
import com.veer.shop.result.AdminMenuResult;
import com.veer.shop.service.admin.AdminMenuService;
import com.veer.shop.utils.ResponseUtil;
/**
 * 后台菜单业务处理
 * 
 * @author WangBin
 * 2020年12月1日-下午6:05:00
 */
@Service
public class AdminMenuServiceImpl implements AdminMenuService {
	
	@Autowired
	private AdminMenuMapper adminMenuMapper;
	
	/**
	 * 后台菜单列表
	 */
	@Override
	public ResponseData<?> getList(AdminMenuParam menuParam) {
		if(menuParam.getWithTree() == 0) {
			// 非树形结构
			List<AdminMenu> menuList = adminMenuMapper.getList(menuParam);
			ListDataResult listDataResult = new ListDataResult();
			listDataResult.setCount((long)menuList.size());
			listDataResult.setList(menuList);
			return ResponseUtil.success(listDataResult);
		} else {
			// 树形结构
			List<AdminMenu> menuList = adminMenuMapper.getList(menuParam);
			List<AdminMenuResult> menutListWithTree = buildMenuTree(menuList, 0);
			return ResponseUtil.success(menutListWithTree);
		}
	}
	
	/**
	 * 构建菜单树
	 *
	 * @param menuList
	 * @param pid
	 * @return
	 */
	private List<AdminMenuResult> buildMenuTree(List<AdminMenu> menuList, Integer pid) {
	    List<AdminMenuResult> treeList = new ArrayList<AdminMenuResult>();
	    menuList.forEach(menu -> {
	    	AdminMenuResult adminMenu = new AdminMenuResult();
	    	adminMenu.setMenuId(menu.getMenuId());
	    	adminMenu.setName(menu.getName());
	    	adminMenu.setParentId(menu.getParentId());
	    	adminMenu.setLevel(menu.getLevel());
	    	adminMenu.setIsActived(menu.getIsActived());
	    	adminMenu.setCreatedAt(menu.getCreatedAt());
	    	adminMenu.setUpdatedAt(menu.getUpdatedAt());
	    	adminMenu.setSortNumber(menu.getSortNumber());
	        if (pid == menu.getParentId()) {
	        	adminMenu.setChildList(buildMenuTree(menuList, menu.getMenuId()));
	            treeList.add(adminMenu);
	        }
	    });
	    return treeList;
	}
	
	/**
	 * 获取菜单详情
	 * @param menuId
	 * @return
	 */
	@Override
	public ResponseData<?> get(Integer menuId) {
		AdminMenu menu = adminMenuMapper.getById(menuId);
		if(menu == null) return ResponseUtil.error(ResponseCodeEnum.GET_ENTITY_NOT_FOUND);
		return ResponseUtil.success(menu);
	}
	
	/**
	 * 新增|更新菜单
	 */
	@Override
	public ResponseData<?> update(AdminMenu adminMenuEntity) {
		int count = 0;
		if(adminMenuEntity.getMenuId() == 0) {
			// 新增
			// 计算level 根据父菜单 level 加1
			AdminMenu parentMenu = adminMenuMapper.getById(adminMenuEntity.getParentId());
			if(parentMenu == null) {
				adminMenuEntity.setParentId(0);
			} else {
				adminMenuEntity.setLevel(parentMenu.getLevel() + 1);
			}
			adminMenuEntity.setUpdatedAt(adminMenuEntity.getCreatedAt());
			Integer maxSortNumber = adminMenuMapper.getMaxSortNumberByParentId(adminMenuEntity.getParentId());
			adminMenuEntity.setSortNumber(maxSortNumber);
			count = adminMenuMapper.insert(adminMenuEntity);
		} else {
			// 更新
			count = adminMenuMapper.update(adminMenuEntity);
		}
		
		if(count > 0) {
			return ResponseUtil.success();
		}
		return ResponseUtil.error();
	}
	
	/**
	 * 删除菜单
	 */
	@Override
	public ResponseData<?> delete(String menuId) {
		String[] menuIds = menuId.split(",");
		boolean flag = false; // 删除出错标识
		for(String id : menuIds) {
			// 判断是否存在子菜单，存在子菜单则不可删除
			List<AdminMenu> childList = adminMenuMapper.getChildById(Integer.parseInt(id));
			if(childList.size() > 0) {
				flag = true;
				break;
			}
			adminMenuMapper.delete(Integer.parseInt(id));
		}
		
		if(flag) {
			return ResponseUtil.error("删除出错：存在子菜单不可删除！");
		}
		
		return ResponseUtil.success();
	}
	
	/**
	 * 获取子菜单列表
	 */
	@Override
	public ResponseData<?> getChidList(AdminMenuParam menuParam) {
		// TODO Auto-generated method stub
		return null;
	}
}
